using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dune.Business.Domain.Content;
using Edge.Common;
using Edge.Common.ComponentModel;
using Edge.Common.Services;

namespace Dune.Web.Controls.Menu
{
    public partial class CalendarMenu : System.Web.UI.UserControl
    {
        private ICollection<NavigationItem> ds;

        public ICollection<NavigationItem> DataSourse
        {
            get { return ds; }
            set { ds = value; }
        }
        
        private class WeekDaysName
        {
            protected string shortName;
            protected string name;

            public string ShortName
            {
                get { return shortName; }
                set { shortName = value; }
            }

            public string Name
            {
                get { return name; }
                set { name = value; }
            }

            public WeekDaysName()
            {
            }

            public WeekDaysName(string shortName, string name)
            {
                this.shortName = shortName;
                this.name = name;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ICollection<WeekDaysName> wdn = new List<WeekDaysName>();
            string[] shortNames = CultureInfo.CurrentCulture.DateTimeFormat.ShortestDayNames;
            string[] fullNames = CultureInfo.CurrentCulture.DateTimeFormat.DayNames;
            for (int i = 1; i < 7; i++)
            {
                WeekDaysName wd = new WeekDaysName(shortNames[i], fullNames[i]);
                wdn.Add(wd);
            }
            WeekDaysName wd0 = new WeekDaysName(shortNames[0], fullNames[0]);
            wdn.Add(wd0);
            rprTitle.DataSource = wdn;
            rprTitle.DataBind();


            lbMonth.DataSource = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
            lbMonth.SelectedValue = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[DateTime.Now.Month-1];
            lbMonth.DataBind();


            int[] years = new int[3];
            int j = 0;
            for (int i = -2; i < 1; i++)
            {
                years[j] = DateTime.Now.Year + i;
                j++;
            }
            lbYear.DataSource = years;
            lbYear.SelectedValue = DateTime.Now.Year.ToString();
            lbYear.DataBind();


         

            ICollection<NavigationItem> month = new List<NavigationItem>();
            int nullDays = 0;
            DateTime firstMonthDate = DateTime.Parse("01/" + DateTime.Now.Month + "/" + DateTime.Now.Year);
            DayOfWeek dayOfWeek = firstMonthDate.DayOfWeek;

            FetchOptions fo = FetchOptions.All;
            fo.SortExpression = "CreateDate";

            NewsFilter nf = new NewsFilter();
            nf.RegisterDateFrom = firstMonthDate;

            ICollection<News> news = Locator.GetService<INewsManager>().Find(fo,nf);

            ICollection<int> daysWithNews = new List<int>();

            foreach (News n in news)
            {
                if (!daysWithNews.Contains(n.CreateDate.Day))    
                    daysWithNews.Add(n.CreateDate.Day);
            }

            int daysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
            switch (dayOfWeek)
            {
                case DayOfWeek.Monday:
                    nullDays = 0;
                    break;
                case DayOfWeek.Tuesday:
                    nullDays = 1;
                    break;
                case DayOfWeek.Wednesday:
                    nullDays = 2;
                    break;
                case DayOfWeek.Thursday:
                    nullDays = 3;
                    break;
                case DayOfWeek.Friday:
                    nullDays = 4;
                    break;
                case DayOfWeek.Saturday:
                    nullDays = 5;
                    break;
                case DayOfWeek.Sunday:
                    nullDays = 6;
                    break;
                default:
                    nullDays = 0;
                    break;

            }

            int weeksCount = (int)Math.Ceiling((decimal)(nullDays+daysInMonth)/7);

            j = 0;
            NavigationItem ni0 = new NavigationItem();
            NavigationItem[] days0 = new NavigationItem[7];
            for (int k = 0; k < nullDays; k++)
            {
                NavigationItem dayItem0 = new NavigationItem();
                dayItem0.Name = "0";
                days0[k] = dayItem0;
            }
            for (int k = nullDays; k < 7; k++)//days.Length; i++)
            {
                NavigationItem dayItem0 = new NavigationItem();
                dayItem0.Name = (j + 1).ToString();
                dayItem0.Current = daysWithNews.Contains(j + 1);
                days0[k] = dayItem0;
                j++;
            }
            ni0.Childrens = days0;
            month.Add(ni0);
            for (int i = 1; i < weeksCount; i++)
            {
                NavigationItem ni = new NavigationItem();
                NavigationItem[] days = new NavigationItem [7];
                for (int k = 0; k < 7; k++)//days.Length; i++)
                {
                    NavigationItem dayItem = new NavigationItem();
                    if (j + 1 <= daysInMonth)
                    {
                        dayItem.Name = (j + 1).ToString();
                        dayItem.Current = daysWithNews.Contains(j + 1);
                    }
                    else
                    {
                        dayItem.Name = "0";
                    }
                    days[k] = dayItem;
                    j++;
                }
                ni.Childrens = days;
                month.Add(ni);
            }
            rprMenu.DataSource = month;
            rprMenu.DataBind();
        }

        protected void rprSubMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            NavigationItem item = (NavigationItem)e.Item.DataItem;
            
            
            if (Convert.ToInt32(item.Name) > 0)
            {
                if (item.Current)
                {
                    HyperLink hlItem = (HyperLink)e.Item.FindControl("hlItem");
                    hlItem.Visible = true;
                }
                else
                {
                    Label lblItem = (Label) e.Item.FindControl("lblItem");
                    lblItem.Visible = true;
                }
            }
        }
    }
}